package com.blockmeta.bbs.immodule.widget;

import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatDialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseTabViewPagerAdapter;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.fragment.HongBaoCreateFragment;

import java.util.ArrayList;
import java.util.List;

import static com.blockmeta.bbs.immodule.Constants.CHATTYPE;
import static com.blockmeta.bbs.immodule.Constants.TOUSERID;

public class HongBaoCreateDialogFragment extends AppCompatDialogFragment {

    TabLayout mTabLayout;
    ViewPager mVp;
    View closeArea;
    List<Fragment> mFragments;
    List<String> tab_title;
    private OnOutPutListener mOnOutPutListener;
    public int mChatType;
    public long mToUserId;
    public ConstraintLayout mHongbaoAnimView;

    public static HongBaoCreateDialogFragment newInstance(int chatType, long toUserId) {
        HongBaoCreateDialogFragment f = new HongBaoCreateDialogFragment();
        f.setArguments(buildArgs(chatType, toUserId));
        return f;
    }

    private static Bundle buildArgs(int chatType, long toUserId) {
        Bundle bundle = new Bundle();
        bundle.putInt(CHATTYPE, chatType);
        bundle.putLong(TOUSERID, toUserId);
        return bundle;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        mChatType = getArguments().getInt(CHATTYPE);
        mToUserId = getArguments().getLong(TOUSERID);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.popup_from_bottom_hongbao, container, false);
        mHongbaoAnimView = (ConstraintLayout) view.findViewById(R.id.hongbao_anim_view);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mVp = (ViewPager) view.findViewById(R.id.vp);
        closeArea = view.findViewById(R.id.v_close_area);
        mFragments = new ArrayList<>();
        tab_title = new ArrayList<>();
        if(mChatType == ChatMessage.MSG_TYPE_UCG) {
            tab_title.add("拼手气红包");
            tab_title.add("均分红包");
            mFragments.add(HongBaoCreateFragment.newInstance(Params.HONGBAO_LUCKY, mChatType, mToUserId));
            mFragments.add(HongBaoCreateFragment.newInstance(Params.HONGBAO_EQUAL, mChatType, mToUserId));
            mTabLayout.addTab(mTabLayout.newTab().setText("拼手气红包"));
            mTabLayout.addTab(mTabLayout.newTab().setText("均分红包"));
        }else {
            tab_title.add("个人红包");
            mFragments.add(HongBaoCreateFragment.newInstance(Params.HONGBAO_PERSON, mChatType, mToUserId));
            mTabLayout.addTab(mTabLayout.newTab().setText("个人红包"));

        }

        //初始化ViewPager
        BaseTabViewPagerAdapter baseTabViewPagerAdapter = new BaseTabViewPagerAdapter(getChildFragmentManager(), mFragments, tab_title);
        mVp.setAdapter(baseTabViewPagerAdapter);
        //预加载数量
        mVp.setOffscreenPageLimit(mFragments.size());
//        ViewUtils.reflex(mTabLayout);
        //设置TabLayout和ViewPager绑定
        mTabLayout.setupWithViewPager(mVp);
        closeArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HongBaoCreateDialogFragment.this.dismiss();
            }
        });

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        int dialogHeight = getContextRect(getActivity());
        if (window != null) {
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), android.R.color.transparent));
            DisplayMetrics dm = new DisplayMetrics();
            if (getActivity() != null) {
                WindowManager windowManager = getActivity().getWindowManager();
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getMetrics(dm);
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.BOTTOM;
                    // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    params.height = DimentionsUtil.dp2px(550);
                    params.dimAmount = 0.5f;
                    params.windowAnimations = R.style.BottomToTopAnim;
                    window.setAttributes(params);
                }
            }
        }
    }

    public void scrollUp() {
//        ViewGroup.LayoutParams layoutParams = mHongbaoAnimView.getLayoutParams();
//        mHongbaoAnimView.
    }

    public void scrollDown() {
//        closeArea.setVisibility(View.VISIBLE);
    }

    public interface OnOutPutListener {
        void onOutPutText(int type, int messageId, String content);
    }


    public static void showDialog(FragmentManager fragmentManager, int chatType, long toUserId, OnOutPutListener onOutPutListener) {
        HongBaoCreateDialogFragment dialog = newInstance(chatType, toUserId);
        dialog.setOnOutPutListener(onOutPutListener);
        dialog.show(fragmentManager, "tag");
    }

    public void setOnOutPutListener(OnOutPutListener onOutPutListener) {
        mOnOutPutListener = onOutPutListener;
    }

    public OnOutPutListener getOnOutPutListener() {
        return mOnOutPutListener;
    }



    //获取内容区域
    private int getContextRect(FragmentActivity activity){
        //应用区域
        Rect outRect1 = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
        return outRect1.height();
    }

    @Override
    public void onDestroy() {
        setOnOutPutListener(null);
        super.onDestroy();
    }
}
